<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/8
 * Time: 8:56
 */

namespace common\business\position;

use common\business\DbHandler;
use common\business\EhrHelper;
use common\business\qywx\QywxDepartmentApiHandler;
use common\helpers\StringUtil;
use common\models\Position;
use DeepCopy\f008\A;
use Yii;
use common\business\QywxDepartmentService;
use common\consts\CommonResources;
use common\consts\Consts;
use common\models\kingdee\OrgPosition;
use common\models\Department;
use common\models\kingdee\OrgAdmin;
use common\models\qywx\QywxDepartment;
use common\result\ActionResult;
use yii\db\Query;
use yii\helpers\Json;

class PositionService
{
    /**
     * @param callback|null $writeBackHandler
     * @return \common\result\ActionResult
     */
    public function sync($writeBackHandler = null){
        $totalCount = OrgPosition::find()->count();

        $totalResult = new ActionResult();

        $db = OrgPosition::getDb();
        $query = (new Query())->from('T_ORG_POSITION TOP')
            ->leftJoin('T_ORG_ADMIN TOA', 'TOA.FID = TOP.FADMINORGUNITID')
            ->leftJoin('T_BD_DOMAIN TBD', 'TBD.FID = TOP.FDOMAINID')
            ->leftJoin('T_ORG_POSITION TOP2', 'TOP2.FID = TOP.FPARENTID')
            ->select([
                'name' => 'TOP.FNAME_L2',
                'number' => 'TOP.FNUMBER',
                'kd_id' => 'TOP.FID',
                'parent_kd_id' => 'TOP.FPARENTID',
                'parent_number' => 'TOP2.FNUMBER',
                'parent_name' => 'TOP2.FNAME_L2',
                'domain' => 'TBD.FNAME_L2',
//                'department_id' => 'TOA.FID',
                'department_number' => 'TOA.FNUMBER',
                'department_kd_id' => 'TOA.FID',
                'department_name' => 'TOA.FNAME_L2',
                'department_display_name' => 'TOA.FDISPLAYNAME_L2',
            ])
            ->orderBy([
                'TOP.FID' => SORT_ASC,
            ]);

        $offset = 0;
        $pageSize = 100;
        $count = $pageSize;

        while ($count >= $pageSize) {
            $all = $query->offset($offset)->limit($pageSize)->all($db);
            $count = count($all);
            $offset += $pageSize;
            foreach ($all as $item){
                $newPosition = new Position();
                $newPosition->setAttributes($item);

                $positionInDb = Position::findOne([
                    'number' => $newPosition->number,
                ]);

                $api = new PositionApiHandler();
                if (!$positionInDb){
                    $totalResult->addSubResult($api->createPosition($newPosition));
                }
                else {
                    $totalResult->addSubResult($api->updatePosition($newPosition, $positionInDb));
                }
                if ($writeBackHandler){
                    $paramArr = [
                        'totalCount' => $totalCount,
                        'finishCount' => $totalResult->total_count,
                        'message' => $totalResult->message,
                    ];
                    call_user_func_array($writeBackHandler, $paramArr);
                }
            }
        }
        return $totalResult;
    }
}